package com.phunware.swipeToDismiss.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.ListView;

/*
 * This is a convenience class to handle the swipe gesture properly.  
 * This is not a required part of the component, but will greatly improve the ease of use.
 *
 * @deprecated: Using this class is no longer necessary.  
 * The adapter makes a call to its parent to disable touch handling for the duration of the touch event
 *
 */
@Deprecated
public class SwipeToDismissListView extends ListView {

	private final GestureDetector gestureDetector;

	public SwipeToDismissListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		gestureDetector = new GestureDetector(context, new XScrollDetector());
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// Call super first because it does some hidden motion event handling
		boolean result = super.onInterceptTouchEvent(ev);
		// Now see if we are not scrolling horizontally with the custom gesture
		// detector
		if (!gestureDetector.onTouchEvent(ev)) {
			return result;
		}
		// If scrolling horizontally (more x than y), don't hijack the event.
		else {
			return false;
		}
	} // Return true if we're scrolling in the x direction

	class XScrollDetector extends SimpleOnGestureListener {
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return Math.abs(distanceY) > Math.abs(distanceX);
		}

	}
}
